package com.boot.rabbitmq.apidemo.model.oneToone;

import com.boot.rabbitmq.apidemo.model.util.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.util.logging.Logger;

/**
 *      简单的一对一生产者队列模型
 */
public  class Send {
    private final static String QUEUE_NAME = "q_test_01";
    static Logger logger = Logger.getLogger(Send.class+"");


    public static void send(String message) throws Exception {
        // 获取到连接以及mq通道
        Connection connection = ConnectionUtil.getConnection();
        // 从连接中创建通道
        Channel channel = connection.createChannel();

        // 声明（创建）队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

        // 消息内容
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        logger.info(" [x] Sent '" + message + "'");
        //关闭通道和连接
        channel.close();
        connection.close();
    }


    public static void main(String[] args) {
        for (int i=100;i>0;i--){
            try {
                send("message_"+i);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }


}
